package com.yb.auth.core.bean;

import com.yb.auth.core.constant.LoginConstant;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Objects;

public class AbstractAuthentication extends AbstractAuthenticationToken {

    private SysUser sysUser;
    private String username;
    private String password;

    private Integer loginType= LoginConstant.LoginType.USER_PASS.getType();//默认手机号登陆

    public AbstractAuthentication(SysUser sysUser){
        super(null);
        this.sysUser=sysUser;
        this.setAuthenticated(false);
        if(Objects.isNull(sysUser))return;
        this.username=sysUser.getUsername();
        this.password=sysUser.getPassword();

    }

    public AbstractAuthentication(String username,String password){
        super(null);
        this.setAuthenticated(false);
        this.username=username;
        this.password=password;
    }
    public AbstractAuthentication(SysUser sysUser, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.sysUser=sysUser;
        this.setAuthenticated(true);
        if(Objects.isNull(sysUser))return;
        this.username=sysUser.getUsername();
        this.password=sysUser.getPassword();

    }

    public static Authentication authenticated(SysUser sysUser, Collection<? extends GrantedAuthority> authorities) {
        return getInstance(sysUser,authorities);
    }

    public static Authentication getInstance(SysUser sysUser,Collection<? extends GrantedAuthority>  authorities){
        Class<?> actualClass = LoginConstant.LoginTokenType.getActualClass(sysUser.loginType().getType());
        if(Objects.isNull(actualClass))return null;
        Constructor<?> constructor = null;
        try {
            constructor  = actualClass.getConstructor(SysUser.class, Collection.class);
            return (Authentication) constructor.newInstance(sysUser,authorities);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void setSysUser(SysUser sysUser) {
        this.sysUser = sysUser;
    }

    public SysUser getSysUser() {
        return sysUser;
    }

    @Override
    public Object getCredentials() {
        return this.password;
    }

    @Override
    public Object getPrincipal() {
        return this.username;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }


    public void setLoginType(Integer loginType) {
        this.loginType = loginType;
    }

    public Integer getLoginType() {
        return loginType;
    }
}
